package com.smile.controller;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.smile.commons.page.R;
import com.smile.model.dto.request.DoLoginRequestDTO;
import com.smile.model.request.DoLoginRequest;
import com.smile.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shopping/auth")
public class AuthController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok("注销成功");
    }

    @PostMapping("/doLogin")
    public SaResult login(@RequestBody DoLoginRequest doLoginRequest) {
        DoLoginRequestDTO doLoginRequestDTO = new DoLoginRequestDTO();
        BeanUtil.copyProperties(doLoginRequest, doLoginRequestDTO);
        Boolean result = loginService.doLogin(doLoginRequestDTO);
        if (result) {
            return SaResult.ok(StpUtil.getTokenValue());
        } else {
            return SaResult.error("登录失败");
        }
    }

    @PostMapping("/test")
    public SaResult test() {
        return SaResult.ok("测试接口");
    }



}
